﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BI.CF.Model;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using BI.CF.PageList;
using BI.CF.IDAL;

namespace BI.CF.DAL
{
    public class Autonym_DAL : Autonym_IDAL
    {
        /// <summary>
        /// 会员实名审核显示
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="CrefiticationTimeMax"></param>
        /// <param name="CrefiticationTimeMin"></param>
        /// <param name="IdCard"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        public PageList.Pager<AutonymModel> GetAutonymShow(string UserName, string MinTime, string MaxTime, string IdCard, int PageIndex, int PageSize)
        {
            SqlParameter[] Parmeter = { new SqlParameter("@UserName", SqlDbType.VarChar),
                                     new SqlParameter("@MinTime",SqlDbType.VarChar),
                                     new SqlParameter("@MaxTime",SqlDbType.VarChar),
                                     new SqlParameter("@IdCard",SqlDbType.VarChar),
                                     new SqlParameter("@PageIndex",SqlDbType.Int),
                                     new SqlParameter("@PageSize",SqlDbType.Int)
                                    };
            Parmeter[0].Value = UserName;
            Parmeter[1].Value = MinTime;
            Parmeter[2].Value = MaxTime;
            Parmeter[3].Value = IdCard;
            Parmeter[4].Value = PageIndex;
            Parmeter[5].Value = PageSize;
            List<AutonymModel> li = new List<AutonymModel>();
            int totalcount = 0;
            DataTable Read = DbHelperSQL.RunProcedure("Proc_AuthonymSelect", Parmeter, PageSize, PageIndex, out totalcount);

            for (int i = 0; i < Read.Rows.Count; i++)
            {
                AutonymModel m = new AutonymModel();
                m.CerfiticationPapers = Read.Rows[i]["CerfiticationPapers"].ToString();
                m.CerfiticationTime = DateTime.Parse(Read.Rows[i]["CerfiticationTime"].ToString());
                m.IdCard = Read.Rows[i]["IdCard"].ToString();
                m.TID = Convert.ToInt32(Read.Rows[i]["TID"].ToString());
                m.UserId = int.Parse(Read.Rows[i]["UserId"].ToString());
                m.UserName = Read.Rows[i]["UserName"].ToString();
                m.RealNameState = Convert.ToBoolean(Read.Rows[i]["RealNameState"]);
                li.Add(m);
            }

            Pager<AutonymModel> show = new Pager<AutonymModel>();
            show.Data = li;
            show.PageIndex = PageIndex;
            show.PageSize = PageSize;
            show.PageCount = (int)Math.Ceiling(Convert.ToDecimal(totalcount) / Convert.ToDecimal(PageSize));
            return show;
        }
        /// <summary>
        /// 添加实名认证审核信息
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public bool Autonym_ADD(AutonymModel m)
        {
            SqlParameter[] Parameter = { new SqlParameter("@UserId", SqlDbType.Int),
                                       new SqlParameter("@CerfiticationTime",SqlDbType.VarChar),
                                       new SqlParameter("@CerfiticationPapers",SqlDbType.VarChar),
                                       new SqlParameter("@IdCard",SqlDbType.VarChar),
                                       new SqlParameter("@RealNameState",SqlDbType.Bit)
                                     };
            Parameter[0].Value = m.UserId;
            Parameter[1].Value = m.CerfiticationTime;
            Parameter[2].Value = m.CerfiticationPapers;
            Parameter[3].Value = m.IdCard;
            Parameter[4].Value = m.RealNameState;
            return DbHelperSQL.RunProcedureReturnBool("PROC_AutonymAdd", Parameter);
        }

        /// <summary>
        /// 删除实名审核信息
        /// </summary>
        /// <returns></returns>
        public bool Autonym_Dele(int id)
        {
            SqlParameter[] Parameter = { new SqlParameter("@TID", SqlDbType.Int) };
            Parameter[0].Value = id;
            return DbHelperSQL.RunProcedureReturnBool("PROC_AutonymDelete", Parameter);
        }
        /// <summary>
        /// 修改实名审核信息
        /// </summary>
        /// <returns></returns>
        public bool Autonym_Upt(AutonymModel m)
        {
            SqlParameter[] Parameter = { new SqlParameter("@TID", SqlDbType.Int),
                                       new SqlParameter("@CerfiticationTime",SqlDbType.VarChar),
                                       new SqlParameter("@RealNameState",SqlDbType.Bit)
                                       };
           
          
          Parameter[1].Value = m.CerfiticationTime;
          Parameter[2].Value = m.RealNameState;
          return DbHelperSQL.RunProcedureReturnBool("PROC_AutonymUpdate", Parameter);
      }



   
        /// <summary>
        /// 根据id查询状态
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public AutonymModel Autonym_SeleID(int ID)
        {
            SqlParameter[] Paramter = { new SqlParameter("@TID", SqlDbType.Int) };
            Paramter[0].Value = ID;
            AutonymModel m = new AutonymModel();
            SqlDataReader dt = DbHelperSQL.RunProcedure("Proc_AuthonymID", Paramter);
            dt.Read();
            
                m.RealNameState = Convert.ToBoolean(dt["RealNameState"].ToString());

            return m;
        }
        /// <summary>
        /// 根据借款查询用户是否实名
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public AutonymModel Get_Authonym_Only(int ID)
        {
            SqlParameter[] Parameter = { new SqlParameter("@UserID", SqlDbType.Int) };
            Parameter[0].Value = ID;
            SqlDataReader dt = DbHelperSQL.RunProcedure("Proc_AutonymSelect", Parameter);
            AutonymModel a = new AutonymModel();
            while (dt.Read())
            {
                a.RealNameState = Convert.ToBoolean(dt["RealNameState"].ToString());
            }
            return a;
        }
    }



}
